<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/06/20
 * Time: 上午10:51
 */
class MaintenanceStationLengthTime extends Eloquent
{
    protected $table = "maintenance_station_length_time";


    /**
     * @Author:      sqq
     * @DateTime:    2018-06-21 11:10:29
     * @Description: 工位占用统计信息入库(基本信息)
     * @Param: $shop_id 门店id
     * @Param: $station_id 工位id
     * @Param: $car_no 车牌号
     * @Return: Object
     */
    public static function SelectOne($shop_id, $station_id, $station_name, $station_type, $occupy_start_time, $occupy_end_time){
        $month = date("Y-m",time());
        $arr = MaintenanceStationLengthTime::where("shop_id",$shop_id)
            ->where("station_id",$station_id)
            ->where('created_at','like','%'.$month.'%')
            ->first();
        //计算此次在工位上停留时长，保存入库
        $length_time = self::str_time($occupy_start_time, $occupy_end_time, $shop_id);
        $length_time = self::format_date($length_time);
        //判断为空添加否则修改
        if(is_null($arr)){
            $arrs = new MaintenanceStationLengthTime();
            $arrs->station_id = $station_id;
            $arrs->station_name = $station_name;
            $arrs->shop_id = $shop_id;
            $arrs->station_type = $station_type;
            $arrs->year_month = date("Y-m", time());
            $arrs->length_time = $length_time;
            $arrs->created_at = date("Y-m-d H:i:s",time());
            $arrs->updated_at = date("Y-m-d H:i:s",time());
            $arrs->save();
        }else{
            $arr->length_time = self::length_time($arr['length_time'],$length_time);
            $arr->updated_at = date("Y-m-d H:i:s",time());
            $arr->save();
        }
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-06-25 下午14:16
     * @Description:     门店工位统计    [ 时间处理-格式化时间转时间戳 ]
     * @param string $start_time        开始时间
     * @param string $end_time        结束时间
     * @link http://www.cms-new.com/report/mcs/str_time
     */
    public static function str_time($start_time, $end_time, $shop_id){

        //首先根据门店ID查询门店营业时间
        $shop = MaintenanceShops::find($shop_id);

        //判断入位时间(总时长)是否超过一天

        $start_time1 = strtotime($start_time);
        $end_time1 = strtotime($end_time);
        $ruwei = $end_time1 - $start_time1;

        if($ruwei > 86400){
            $days_h_m_s = self::Ym($ruwei);
            $days = strtok(trim($days_h_m_s) ,' '); //去除空格，并且获取空格以前的数据
            $business_start_time = strtotime($shop->business_start_time.":00");
            $business_end_time = strtotime($shop->business_end_time.":00");
            $business = $business_end_time - $business_start_time;
            if($business <= 0){
                $business = 0;
            }
            $time = $days * ($business);
        }else{
            $time = 0;
        }

        $where['start_time'] = $start_time;
        $where['end_time'] = $end_time;
        //判断是否在营业时间内
        $time1 = self::get_curr_time_section($shop->business_start_time, $shop->business_end_time, $where);
//        if(!is_numeric($where['end_time'])){
//            $end_time1 = strtotime($where['end_time']);
//        }
//
//        if(!is_numeric($where['start_time'])){
//            $start_time1 = strtotime($where['start_time']);
//        }
//        $time = $end_time1 - $start_time1;

        return $time + $time1;
    }

    /**
     * @Author:          sqq
     * @DateTime:        2018-06-25 下午14:16
     * @Description:     门店工位统计    [ 时间处理-00:00格式]
     * @param string $time        开始时间   （精确到秒）
     * @link http://www.cms-new.com/report/mcs/format_date
     */
    public static function format_date($time){

        //$a_time = $time;
//        $time = sprintf('%02s', floor($time/(int)3600));
//        if($time <= 00){
//            $time = sprintf('%02s',$time).":".sprintf('%02s', floor($a_time/(int)60));
//        }else{
//            $time = $time.":".sprintf('%02s', floor(($a_time-$time*3600)/(int)60));
//        }
        if(is_numeric($time)){
            $value = array(
                //"years" => 0, "days" => 0,
                "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
//            if($time >= 31556926){
//                $value["years"] = floor($time/31556926);
//                $time = ($time%31556926);
//            }
//            if($time >= 86400){
//                $value["days"] = floor($time/86400);
//                $time = ($time%86400);
//            }
            if($time >= 3600){
                $value["hours"] = floor($time/3600);
                $time = ($time%3600);
            }
            if($time >= 60){
                $value["minutes"] = floor($time/60);
                $time = ($time%60);
            }
            $value["seconds"] = floor($time);
            //return (array) $value;
            $t= sprintf('%02s',$value["hours"]) .":". sprintf('%02s',$value["minutes"]) .":".sprintf('%02s',$value["seconds"]);

            return $t;
        }else{
            return '00:00:00';
        }
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-06-25 下午14:16
     * @Description:     门店工位统计    [ 时间处理-00:00+01:01 ]
     * @param string $start_time        开始时间
     * @param string $end_time        结束时间
     * @link http://www.cms-new.com/report/mcs/length_time
     */
    public static function length_time($new_time, $old_time){

        if($old_time == 0){
            $old_time = '00:00:00';
        }
        if($new_time == 0){
            $new_time = '00:00:00';
        }
        $new_time = explode(":",$new_time);
        $old_time = explode(":",$old_time);
        $time = $new_time[0] + $old_time[0];
        $time1 = $new_time[1] + $old_time[1];
        $time2 = $new_time[2] + $old_time[2];
        if($time2 >= 60){
            $time_carry2 = floor($time2/(int)60);
            $time2 = $time2 - $time_carry2*60;
            $time2 = $time2 + $time_carry2;
        }
        if($time1 >= 60){
            $time_carry = floor($time1/(int)60);
            $time1 = $time1 - $time_carry*60;
            $time = $time + $time_carry;
        }
        return sprintf('%02s',$time).":".sprintf('%02s',$time1).":".sprintf('%02s',$time2);
    }

    /**
     * @Author:          sqq
     * @DateTime:        2018-06-27 上午08:28
     * @Description:     门店工位统计    [ 时间处理-判断是否在门店营业范围之内 ]
     * @param string $start_time        开始时间
     * @param string $end_time        结束时间
     * @link http://www.cms-new.com/report/mcs/get_curr_time_section
     */
    public static function get_curr_time_section($business_start_time, $business_end_time, $where)
    {
        $business_start_time = strtotime($business_start_time.":00");
        $business_end_time = strtotime($business_end_time.":00");

        $start_time =strtotime(explode(' ',trim($where['start_time']))[1]);
        $end_time =strtotime(explode(' ',trim($where['end_time']))[1]);
        if($end_time >= $start_time){
            if($end_time <= $business_start_time || $start_time >= $business_end_time){
                $time = 0;
            }elseif($start_time <= $business_start_time){
                $z_start_time = $business_start_time;
                if($end_time >= $business_end_time){
                    $z_end_time = $business_end_time;
                }else{
                    $z_end_time = $end_time;
                }
                $time = $z_end_time - $z_start_time;
            }elseif($business_start_time < $start_time && $start_time <= $business_end_time){
                $z_start_time = $business_start_time;
                if($end_time >= $business_end_time){
                    $z_end_time = $business_end_time;
                }else{
                    $z_end_time = $end_time;
                }
                $time = $z_end_time - $z_start_time;
            }
        }elseif($end_time < $start_time){
            if($end_time >= $business_end_time || $start_time <= $business_start_time){
                $time = $business_end_time - $business_start_time;
            }elseif($end_time >= $business_start_time){
                $station_time1 = $end_time - $business_start_time;
                if($start_time >= $business_end_time){
                    $station_time2 = 0;
                }else{
                    $station_time2 = $business_end_time - $start_time;
                }
                $time = $station_time1 + $station_time2;
            }elseif($end_time < $business_start_time){
                if($start_time >= $business_start_time && $start_time < $business_end_time) {
                    $time = $business_end_time - $start_time;
                }else{
                    $time = 0;
                }
            }else{
                $time = 0;
            }
        }else{
            $time = 0;
        }

        return $time;
        //记录的开始时间 < = 开始营业时间
//        if($start_time <= $business_start_time){
//            $z_start_time = $business_start_time;
//            //记录的结束时间 <= 开始营业时间
//            if($end_time <= $business_start_time){
//                $z_end_time = $business_start_time;
//            //开始营业时间 < 记录的结束时间 <= 结束营业时间
//            }elseif($business_start_time < $end_time && $end_time  <= $business_end_time){
//                $z_end_time = $end_time;
//            }else{
//                $z_end_time = $business_end_time;
//            }
//        }elseif($business_start_time < $start_time && $start_time <= $business_end_time){
//            $z_start_time = $start_time;
//            if($end_time <= $business_end_time){
//                $z_end_time =  $end_time;
//            }else{
//                $z_end_time = $business_end_time;
//            }
//        }else{
//            if($end_time <= $business_start_time){
//                $z_start_time = $business_end_time;
//                $z_end_time = $business_end_time;
//            }elseif($business_start_time < $end_time && $end_time <= $business_end_time){
//                $z_start_time = $business_start_time;
//                $z_end_time = $end_time;
//            }
//        }

        // return array('start_time'=>$z_start_time,'end_time'=>$z_end_time);
    }


    public static function Ym($time){

        if(is_numeric($time)){
            $value = array(
                //"years" => 0, "
                "days" => 0,
                "hours" => 0,
                "minutes" => 0,
                "seconds" => 0,
            );
//            if($time >= 31556926){
//                $value["years"] = floor($time/31556926);
//                $time = ($time%31556926);
//            }
            if($time >= 86400){
                $value["days"] = floor($time/86400);
                $time = ($time%86400);
            }
            if($time >= 3600){
                $value["hours"] = floor($time/3600);
                $time = ($time%3600);
            }
            if($time >= 60){
                $value["minutes"] = floor($time/60);
                $time = ($time%60);
            }
            $value["seconds"] = floor($time);
            //return (array) $value;
            $t= sprintf('%02s',$value["days"]) ." ".sprintf('%02s',$value["hours"]) .":". sprintf('%02s',$value["minutes"]) .":". sprintf('%02s',$value["seconds"]);
            return $t;
        }else{
            return '00:00:00';
        }
    }


}
